package com.imooc.maokeyang.alllearning.controller;


import com.imooc.maokeyang.alllearning.domain.entity.UserDO;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@Api(tags = "测试API接口")
public class QuickStartController {

    @RequestMapping(value = "/test", method = RequestMethod.GET, headers="Referer=http://www.ifeng.com/")
    public void testHeaders(@PathVariable String ownerId, @PathVariable String petId) {
        // implementation omitted
    }

    @RequestMapping(value = "/response/ContentType", headers = "Accept=application/json")
    public void response2(HttpServletResponse response) throws IOException {
        //表示响应的内容区数据的媒体类型为json格式，且编码为utf-8(客户端应该以utf-8解码)
        response.setContentType("application/json;charset=utf-8");
        //写出响应体内容
        String jsonData = "{\"username\":\"zhang\", \"password\":\"123\"}";
        response.getWriter().write(jsonData);
    }


    @RequestMapping(value = "/test/{userId}", method = RequestMethod.GET, params="myParam=myValue")
    public UserDO findUser(@PathVariable String userId) {
        // implementation omitted

        return UserDO.builder().username("wanghan").password("123").build();
    }


    /**
     * /test?userId=123&code=100
     *
     * @param userId
     * @param code
     * @return
     */
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public UserDO findUser2(@RequestParam(name = "userId", defaultValue = "1", required = true) String userId,
                            @RequestParam(name = "code", defaultValue = "1", required = true) String code
    ) {
        // implementation omitted

        return UserDO.builder().username("wanghan").password("123").build();
    }

}


